! MainMenu.f 
! Created 8/27/91 9:37 by AppMaker 

!!MP inlines.f
!!G JLtest.finc.f

!!D+
!!R+
!!OV+
!!S MainMenu 

!----------
	Subroutine InitTitles
	Call InitFileM
	Call InitEditM
	End !InitTitles
 
!----------
	Subroutine LoadMenus
	include 'Mainmenu.inc'
	AppleMenu.menuh	= GetMenu (AppleID)
	Call AddResMenu (AppleMenu, %val('DRVR'))
	FileMenu.menuh	= GetMenu (FileID)
	EditMenu.menuh	= GetMenu (EditID)
	Call InsertMenu (AppleMenu, INT2(0))
	Call InsertMenu (FileMenu, INT2(0))
	Call InsertMenu (EditMenu, INT2(0))
		Call DrawMenuBar
	End !LoadMenus

!----------
	Subroutine DoDeskAcc (itemNr)
	integer*2 itemNr,refNum
	String*255 name
	include 'Mainmenu.inc'
	Call GetItem (AppleMenu, itemNr, name)
	refNum = OpenDeskAcc (name)
	End !DoDeskAcc

!----------
	Subroutine DoAbout 
	integer*2 itemNr
		itemNr = Alert (AboutID, nil)
	End !DoAbout 
!----------
	Subroutine DoApple (itemNr)
	integer*2 itemNr
	Select case (itemNr)
	case(AppleAbout)
		Call DoAbout 
	
		case default
		Call DoDeskAcc (itemNr)
	end select
	End !DoApple
!----------
	Subroutine DoMenus (menuChoice)
	integer*2 menuID, itemNr
	integer*4 menuChoice,M1,M2,PortTemp
	PortTemp = 32767
	M1 = JIAND(MENUCHOICE,PortTemp)	!extract the menu & item numbers
	M2 = JISHFT(MENUCHOICE,-16)
	menuID = M2
	itemNr = M1

	if (menuID = AppleID) Call DoApple (itemNr)
	if (menuID = FileID) Call DoFile (itemNr)
	if (menuID = EditID) Call DoEdit (itemNr)
	
	Call HiliteMenu (INT2(0))
	End !DoMenus
 
!----------
	Subroutine UpdateMenus
	include 'Globals.inc'
	pointer /WindowRecord/	frontPeek
	logical	isFront				!is there a front window?
	logical	isCur				!is there a current window?
	logical	isDirty				!is it dirty?
	logical	hasFile				!does it have a file?
	logical	isSelected			!is anything selected?
	logical	isDesk				!is the front window a desk acc?
	logical	isText				!is there a current text field?
	logical	isScrap				!is there any scrap?
	record /MenuHandle/	menu			
	logical	menuBarChanged
	logical EnableTitle
	external EnableTitle
	
	menuBarChanged = .false.
	isFront		= (FrontWindow <> nil)
	isCur		= (curWindow <> nil)
	isDirty 	= .false.
	hasFile 	= .false.
	isSelected	= .false.
	if (isCur = .true.) then 
		isDirty		=  cur^.dirty
		hasFile		= (cur^.fileNum <> 0)
	end if

	isDesk = .false.
	if (isFront = .true.) then 
		frontPeek	= FrontWindow()
		isDesk		= (frontPeek^.windowKind < 0)
	end if
	isText		= .false.
	if (isCur = .true.) then 
		isText		= (cur^.text.TEH <> nil)
	end if
	isScrap		= .false.
	if (isText = .true.) then 
		isSelected	= (cur^.text.TEH^.TEP^.selStart <> cur^.text.TEH^.TEP^.selEnd)
		isScrap		= (TEGetScrapLen > 0)
	end if
	
	menu.menuH = FileMenu.menuH
	Call Enable (menu, FileClose, isFront)
	Call Enable (menu, FileSave, isDirty)
	Call Enable (menu, FileSaveAs, hasFile)
	Call Enable (menu, FileRevert, isDirty .and. hasFile)
	menu.menuH = EditMenu.menuH
	if (isFront = .true.) then 
		Call Enable (menu, EditUndo,isDesk)
		Call Enable (menu, EditCut,isDesk .or. isSelected)
		Call Enable (menu, EditCopy,isDesk .or. isSelected)
		Call Enable (menu, EditPaste,isDesk .or. isScrap)
		Call Enable (menu, EditClear,isDesk .or. isSelected)
		Call Enable (menu, EditSelectAll,isText)
		end if
	If (EnableTitle (EditMenu, isFront)) menuBarChanged = .true.
	 
	if (menuBarChanged) Call DrawMenuBar

	End !UpdateMenus

	!----------
	Subroutine Enable (menu, itemNr, enabled)
	integer*2 itemNr
	Integer*4 menu
	logical enabled	
		if (enabled) then 
			Call EnableItem  (menu, itemNr)
		else 
			Call DisableItem (menu, itemNr)
		end if
	End !Enable
	
	!----------
	Logical Function EnableTitle (menu, enabled)
	record /MenuHandle/ menu
	Integer*4 enabled
	
		EnableTitle = .false.	
		if (Abs(enabled) <> (IIAnd(menu.menuH^.menuP^.enableFlags, INT2(1)))) EnableTitle = .true.
		if (enabled) then 
			Call EnableItem  (menu, INT2(0))
		else 
			Call DisableItem (menu, INT2(0))
		end if
	End !EnableTitle
	
